package cn.gzsxt.dao.impl;

import java.util.List;

import com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException;

import cn.gzsxt.dao.TaskDao;
import cn.gzsxt.po.PageOut;
import cn.gzsxt.po.TaskDetail;

public class TaskDaoImpl extends MyBaseDaoImpl implements TaskDao {
	
	@Override
	public TaskDetail getRow(Integer id) {
		String sql = "select * from t_tasks where taskid ="+id;
		return super.getRow(TaskDetail.class ,sql);
	}

	@Override
	public List getSeletctRows(PageOut page) {
		String sql = getSql(page);
		return super.getSelectRows(TaskDetail.class , sql);
	}

	private String getSql(PageOut page) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from t_tasks");
		String keyWord = page.getKeyword();
		System.out.println(keyWord);
		boolean isKeyword = keyWord != null && keyWord.trim().length() != 0;
		
		if (isKeyword){
			sql.append(" where taskTitle like '%"+keyWord+"%' or clerk like '%"+keyWord+"%' or Status like '%"+keyWord+"%' or taskContent like '%"+keyWord+"%'");
			System.out.println(sql);
		}
		if (page.isPage()){
			sql.append(" limit "+page.getBeginIndex()+","+page.getEndIndex());
		}
		return sql.toString();
	}

	@Override
	public int updTask(TaskDetail task) {
		return super.update(task);
	}

	@Override
	public int delTask(String ids) {
		String sql = "delete from t_tasks where taskId in ("+ids+")";
		return super.delete(sql);
	}

	@Override
	public int addTask(TaskDetail task) {
		try {
			return super.insert(task);
		} catch (MySQLSyntaxErrorException e) {
			e.printStackTrace();
		}
		return 0;
	}


}
